function TXWawaReporter(){};

TXWawaReporter.prototype = {
  APIURL : 'http://123.206.118.43:8099/wawaji/logreport',
  method : 'POST',
  //事件时机
  events : {
    first_pkg : "first_pkg",    //收到第一个视频数据包
    main_switch: "main_switch", //切换至主路
    side_switch: "side_switch", //切换至辅路
    main_first_render : "main_first_render", //主路首帧渲染时
    side_first_render : "side_first_render",  //辅路首帧渲染时
    endPlay : "endPlay",        //游戏结束
    get_sig_begin : "get_sig_begin", //获取sig开始     
    get_sig_end : "get_sig_end", //获取sig成功 获取sig失败|错误码：x|错误信息：x    
    login_begin : "login_begin", //登陆开始        
    login_end : "login_end", //登陆成功    登陆失败|错误码：x|错误信息：x   
    get_rtmp_address_begin : "get_rtmp_address_begin", //获取rtmp地址        
    get_rtmp_address_end : "get_rtmp_address_end", //获取rtmp地址成功  获取rtmp地址失败|错误码：x|错误信息：xxx   
    get_ws_begin : "get_ws_begin", //获取ws地址开始        
    get_ws_end : "get_ws_end", //获取ws地址成功    获取ws地址失败|错误码：x|错误信息：xxx 
    queue_begin : "queue_begin", //排队开始        
    queue_end : "queue_end", //排队成功    排队失败|错误码：x|错误信息：xxx 
    queue_num : "queue_num", //排队中     
    enter_room_begin : "enter_room_begin", //进房开始        
    enter_room_end : "enter_room_end", //进房成功    进房失败|错误码：x|错误信息：xxx 
    coin_begin : "coin_begin", //投币开始        
    coin_end : "coin_end", //投币成功    投币失败|错误码：x|错误信息：xxx 
    control_rtt : "control_rtt", //操作延迟|连接：x|操作：xxx        
    catch_result : "catch_result", //抓取结果：x      
    quit_room_begin : "quit_room_begin", //退房开始        
    quit_room_end : "quit_room_end",  //退房结束        
    connection_close : "connection_close", //连接中断|错误码：x|错误信息：x       
    first_frame : "first_frame", //首帧      
    rtmp_begin : "rtmp_begin", //rtmp拉取开始        
    rtmp_first_frame : "rtmp_first_frame", //rtmp首帧      
    rtmp_fail : "rtmp_fail", //rtmp拉取失败        
    },
    levels : {
      info : 'info',
      warn : 'warn',
      error : 'error'
    },
  
    defDatas : {
      "appid": 0,
      "roomid": 0,
      "userid":"",
      "weburl":"",
      "log_key":"",
      "log_value":0,
      "log_msg":"",
      "log_level":"",
      "playid": "",
      "time":0
    },
  
    setAppid : function (id) {
        if (id==undefined || id==null) {
            console.log('appid invalid!');
            return;
        }
        this.defDatas.appid = id;
    },
  
    setRoomId : function (id) {
        if (id==undefined || id==null) {
            console.log('roomid invalid!');
            return;
        }
        this.defDatas.roomid = id;
    },
  
    setUserId : function (id) {
        if (id==undefined || id==null) {
            console.log('userid invalid!');
            return;
        }
        this.defDatas.userid = id;
    },
    setPlayId : function (id) {
        if (id==undefined || id==null) {
            console.log('playid invalid!');
            return;
        }
        this.defDatas.playid = id;
    },
  
    send : function(datas) {
      try {
        datas = this.fillDatas(datas);
        var xhr = this.getXHR();
        xhr.onreadystatechange = this.reporterHandel(this);
        xhr.open(this.method , this.APIURL , true);
        // xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
        // xhr.setRequestHeader('Access-Control-Allow-Headers', 'X-Requested-With');
        // xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, PUT');
        // xhr.setRequestHeader('Content-Type' , 'application/json;charset=utf-8');
        // xhr.setRequestHeader('Content-Type' , 'application/x-www-form-urlencoded');
        //xhr.send(self.datasParseToString(datas));
        xhr.send(JSON.stringify(datas));
      } catch (e) {
        console.log(e);
      }
    },
  
    fillDatas : function (datas) {
      var self = this;
      var dt = {};
      for (var p in self.defDatas) {
        if (['appid','roomid','userid','playid'].indexOf(p)>=0){
          dt[p] = self.defDatas[p];
          continue;
        }
        dt[p] = typeof datas[p]!='undefined' ? datas[p] : self.defDatas[p];
      }
      return dt;
    },
  
    datasParseToString : function (datas) {
      var tmp = [];
      for (var p in datas) {
          tmp.push(p+'='+encodeURIComponent(datas[p]));
      }
      return tmp.join('&');
    },
  
    getXHR : function (){
      var xmlhttp=null;
      if (window.XMLHttpRequest) {// code for all new browsers
        xmlhttp=new XMLHttpRequest();
      } else if (window.ActiveXObject) {// code for IE5 and IE6
        try{
              xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }catch (e)  {
              try{
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              }catch (e){}
            }
      }
      if (xmlhttp==null){
        console.log("Your browser does not support XMLHTTP.");
        return;
      }
      return xmlhttp;
    },
    reporterHandel : function () {
      return function (ev) {
        if(ev &&  ev.readyState ){
          switch (ev.readyState) {
            case 0:
  
            break;
            case 1:
  
            break;
            case 2:
  
            break;
            case 3:
  
            break;
            case 4:
  
            break;
          }
        }
      }
    },
    log: function( key , value ){
      this.send({
        'log_key' : key,
        'log_value' : value || 0,
        'log_msg' : key,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    warn: function( key , value ){
      this.send({
        'log_key' : key,
        'log_value' : value || 0,
        'log_msg' : key,
        'log_level' : this.levels.warn,
        'time' : +new Date()
      });
    },
    error: function( key , value ){
      this.send({
        'log_key' : key,
        'log_value' : value || 0,
        'log_msg' : key,
        'log_level' : this.levels.error,
        'time' : +new Date()
      });
    },
    onJoinRoom : function () {
      this.send({
        'log_key' : this.events.enter_room_begin,
        'log_value' : 0,
        'log_msg' : this.events.enter_room_begin,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    afterJoinRoom : function () {
      this.send({
        'log_key' : this.events.enter_room_end,
        'log_value' : 0,
        'log_msg' : this.events.enter_room_end,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    afterFirstPkg : function () {
      this.send({
        'log_key' : this.events.firstPkg,
        'log_value' : 0,
        'log_msg' : this.events.firstPkg,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    //主路第一次渲染
    afterMFirstRend : function () {
      this.send({
        'log_key' : this.events.mFirstRend,
        'log_value' : 0,
        'log_msg' : this.events.mFirstRend,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    //辅路第一次渲染
    afterSFirstRend : function () {
      this.send({
        'log_key' : this.events.sFirstRend,
        'log_value' : 0,
        'log_msg' : this.events.sFirstRend,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    },
    //退出
    afterEndPlay : function () {
      this.send({
        'log_key' : this.events.endPlay,
        'log_value' : 0,
        'log_msg' : this.events.endPlay,
        'log_level' : this.levels.info,
        'time' : +new Date()
      });
    }
  };
  